// pages/jielong/order/order.js
var template = require('../template/tabber/tabber.js');
var app = getApp()
var jlpay = app.data.jlpay;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nav:['已参与','已完成'],
    selectIndex: 0,
    noorder:false,
    hasorder:true,
    dataList:null,
    ptime:[],
    text:'您还没有完成的接龙'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中',
    })
    this.getList(0)
    var that=this;
    template.tabbar("tabBar", 1, this);
    wx.getStorage({
      key: 'isTz',
      success: function(res) {
        if(res.data==1){
          that.setData({
            isTz:true
          })
        } else {
          that.setData({
            isTz: false
          })
        }
      },
    })
  },
  gogood:function(e){
    wx.navigateTo({
      url: '../ActiveCommodityStatistics/ActiveCommodityStatistics?id=' + e.currentTarget.dataset.activeid,
    })
  },
  timestampToTime: function(timestamp) {
    var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = date.getDate() + ' ';
    var h = date.getHours() + ':';
    var m = date.getMinutes() + ':';
    var s = date.getSeconds();
    return Y + M + D + h + m + s;
  },
  getList: function (type) {
    let that = this
    wx.request({
      url: jlpay + 'op=getUnverifActiveList',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        openid: wx.getStorageSync('openid'),
        unionId: app.data.unionId || wx.getStorageSync('unionId'),
        type: type
      },
      success: function (res) {
        wx.hideLoading()
        console.log(res)
        if(res.data.data.length==0){
          that.setData({
            hasorder:false
          })
        }else{
          that.setData({
            hasorder: true
          })
        }
        if (type==0){
          that.setData({
            text:'您还没有已参与的接龙'
          })
        }else{
          that.setData({
            text: '您还没有已完成的接龙'
          })
        }
        let ptimes = []
        let date = new Date().getTime()
        // console.log(res.data.data[0].goods)
        for(let i=0;i<res.data.data.length;i++){
            ptimes.push(that.timestampToTime(res.data.data[i].ptime))  
        }
        console.log(ptimes)
        that.setData({
          ptime: ptimes,
          dataList: res.data.data
        })
      }
    })
  },
  goorderdetail:function(e){
    wx.navigateTo({
      url: '../orderdetail/orderdetail?orderno=' + e.currentTarget.dataset.index,
    })
  },
  goactivity: function (e) {
    console.log(e)
    wx.navigateTo({
      url: '../ActivityStatistics/ActivityStatistics?id=' + e.currentTarget.dataset.activeid,
    })
  },
  selectNav: function (e) {
    this.setData({
      selectIndex: e.currentTarget.dataset.index
    })
    this.getList(e.currentTarget.dataset.index)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },
  
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  share:function(e){
    console.log(e)
    console.log(2)
      wx.showShareMenu({
        withShareTicket: true
      })
  },
  onShareAppMessage: function () { //分享页面
    var tzInfo = JSON.stringify(wx.getStorageSync('tzInfo'));
    return {
      title: '',
      path: '/pages/jielong/products/products?activeid=' + this.data.id + "&activestatus=" + this.data.status + '&tzInfo=' + tzInfo,
      success: function (res) {
        console.log(res);
        console.log("转发成功:" + JSON.stringify(res));
      }
    }
  },
})